﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using DTO;
using System.Net;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Text;
using System.Net;

namespace DAO
{

    public class TourDAO
    {
        /// <summary>
        /// Lay danh sach tat ca tour theo link web service
        /// </summary>
        /// <param name="wsLink">Web Service Link</param>
        /// <returns></returns>
        public List<TourDTO> layDanhSachTour(string wsLink, string username, string password, string tourType, string departurePostition, string visitLocation)
        {
            try
            {
                //WebServiceInvoker invoker = new WebServiceInvoker(new Uri(wsLink));

                //string service = "WSTravel";
                //string method = "WSTravel1";

                //string[] args = new string[] { "", "", "" };

                //ListTour[] result = invoker.InvokeMethod<ListTour[]>(service, method, args);
                //throw new Exception();

                G02TravelWebService.WSTravel service = new G02TravelWebService.WSTravel();
                service.Url = wsLink;
                service.CookieContainer = new System.Net.CookieContainer();
                service.WSTravel0(username, password);

                G02TravelWebService.ListTour[] result = service.WSTravel1(tourType, departurePostition, visitLocation);
                return this.convertToTourDTO(result);

                ////Gọi Web Service bằng cách gửi các HTTP request

                //1. Tạo nội dung của HTTP Request
                //string url = "http://www.g02travel.somee.com/WS/WSTravel.asmx/WSTravel1?TourType=&Departure=&Destination=";
                //string contentType = "application/x-www-form-urlencoded";
                //string method = "POST";
                //string content = "";

                //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                //req.ContentType = contentType;
                //req.Method = method;
                //req.ContentLength = content.Length;

                //Stream s = req.GetRequestStream();
                //StreamWriter sw = new StreamWriter(s, Encoding.ASCII);
                //sw.Write(content);
                //sw.Close();

                ////2. Lấy kết quả trả về
                //HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                //string response = GetResponseAsString(res);

                ////3. Xử lý kết quả trả về
                //StringReader xmlReader = new StringReader(response);

                //DataSet ds = new DataSet();

                //ds.ReadXml(xmlReader);
                //return null;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ListTourDetailDTO layThongTinChiTietTour(string wsLink, string username, string password, string tripID)
        {
            try
            {
                G02TravelWebService.WSTravel service = new G02TravelWebService.WSTravel();
                service.Url = wsLink;
                service.CookieContainer = new System.Net.CookieContainer();
                service.WSTravel0(username, password);

                G02TravelWebService.ListTourDetail result = service.WSTravel3(tripID);
                if (result != null)
                {
                    return this.convetToListTourDetailDTO(result);
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<TourDTO> timKiemTourMoRong(string wsLink, string username, string password, string tourtype, string location, float fromPrice, float toPrice, DateTime fromDate, DateTime toDate)
        {
            try
            {
                //WebServiceInvoker invoker = new WebServiceInvoker(new Uri(wsLink));

                //string service = "WSTravel";
                //string method = "WSTravel1";

                //string[] args = new string[] { "", "", "" };

                //ListTour[] result = invoker.InvokeMethod<ListTour[]>(service, method, args);
                //throw new Exception();

                G02TravelWebService.WSTravel service = new G02TravelWebService.WSTravel();
                service.Url = wsLink;
                service.CookieContainer = new System.Net.CookieContainer();
                service.WSTravel0(username, password);

                G02TravelWebService.ListTour[] result = service.WSTravel2(tourtype, fromDate, toDate, fromPrice, toPrice, location);
                return this.convertToTourDTO(result);

                ////Gọi Web Service bằng cách gửi các HTTP request

                //1. Tạo nội dung của HTTP Request
                //string url = "http://www.g02travel.somee.com/WS/WSTravel.asmx/WSTravel1?TourType=&Departure=&Destination=";
                //string contentType = "application/x-www-form-urlencoded";
                //string method = "POST";
                //string content = "";

                //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                //req.ContentType = contentType;
                //req.Method = method;
                //req.ContentLength = content.Length;

                //Stream s = req.GetRequestStream();
                //StreamWriter sw = new StreamWriter(s, Encoding.ASCII);
                //sw.Write(content);
                //sw.Close();

                ////2. Lấy kết quả trả về
                //HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                //string response = GetResponseAsString(res);

                ////3. Xử lý kết quả trả về
                //StringReader xmlReader = new StringReader(response);

                //DataSet ds = new DataSet();

                //ds.ReadXml(xmlReader);
                //return null;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //cac ham ho tro
        public string GetResponseAsString(WebResponse res)
        {
            Stream s = res.GetResponseStream();
            //StreamReader sr = new StreamReader(s, Encoding.ASCII);
            StreamReader sr = new StreamReader(s, Encoding.UTF8);
            StringBuilder sb = new StringBuilder();
            char[] data = new char[1024];
            int nBytes;
            do
            {
                nBytes = sr.Read(data, 0, (int)1024);
                for (int i = 0; i < nBytes; i++)
                    sb.Append(data[i]);
            } while (nBytes == 1024);
            return sb.ToString();
        }

        public List<TourDTO> convertToTourDTO(G02TravelWebService.ListTour[] tourArr)
        {
            List<TourDTO> tourLst = new List<TourDTO>();
            for (int i = 0; i < tourArr.Length; i++)
            {
                TourDTO tour = new TourDTO();
                tour.DayNight = tourArr[i].DayNight;
                tour.DeparturePosition = tourArr[i].DeparturePosition;
                tour.Description = tourArr[i].Decription;
                tour.EndDay = tourArr[i].EndDay;
                tour.Quantity = tourArr[i].Quatity;
                tour.Price = tourArr[i].Price;
                tour.StartDay = tourArr[i].StartDay;
                tour.TourImage = tourArr[i].TourImage;
                tour.TourName = tourArr[i].TourName;
                tour.TourType = tourArr[i].TourType;
                tour.TripId = tourArr[i].TripId;
                tour.Unit = tourArr[i].Unit;
                tour.VisitLocation = tourArr[i].VisitLocation;
                tourLst.Add(tour);
            }
            return tourLst;
        }

        public ListTourDetailDTO convetToListTourDetailDTO(G02TravelWebService.ListTourDetail tour)
        {
            ListTourDetailDTO tourDet = new ListTourDetailDTO();
            tourDet.ListTour = new TourDTO();
            tourDet.ListTour.DayNight = tour.ListTour.DayNight;
            tourDet.ListTour.DeparturePosition = tour.ListTour.DeparturePosition;
            tourDet.ListTour.Description = tour.ListTour.Decription;
            tourDet.ListTour.EndDay = tour.ListTour.EndDay;
            tourDet.ListTour.Price = tour.ListTour.Price;
            tourDet.ListTour.Quantity = tour.ListTour.Quatity;
            tourDet.ListTour.StartDay = tour.ListTour.StartDay;
            tourDet.ListTour.TourImage  = tour.ListTour.TourImage;
            tourDet.ListTour.TourName = tour.ListTour.TourName;
            tourDet.ListTour.TourType = tour.ListTour.TourType;
            tourDet.ListTour.TripId = tour.ListTour.TripId;
            tourDet.ListTour.Unit = tour.ListTour.Unit;
            tourDet.ListTour.VisitLocation = tour.ListTour.VisitLocation;            

            List<ScheduleDTO> sche = new List<ScheduleDTO>();
            for (int i = 0; i < tour.Schedule.Length; i++)
            {
                ScheduleDTO schedule = new ScheduleDTO();
                schedule.Airplane = tour.Schedule[i].Airplane;
                schedule.Description = tour.Schedule[i].Decription;
                schedule.Hotel = tour.Schedule[i].Hotel;
                schedule.Location = tour.Schedule[i].Location;
                schedule.ScheduleId = tour.Schedule[i].ScheduleId;
                schedule.Title = tour.Schedule[i].Title;
                schedule.TitleImage = tour.Schedule[i].TitleImage;
                schedule.Transport = tour.Schedule[i].Transport;
                sche.Add(schedule);
            }
            List<TicketDTO> tickLst = new List<TicketDTO>();
            for (int i = 0; i < tour.Ticket.Length; i++)
            {
                TicketDTO tick = new TicketDTO();
                tick.Adult = tour.Ticket[i].Adult;
                tick.Child = tour.Ticket[i].Child;
                tick.CusType = tour.Ticket[i].CusType;
                tickLst.Add(tick);
            }

            tourDet.Schedule = sche;
            tourDet.Ticket = tickLst;
            return tourDet;
        }

        public List<LocationDTO> layDiaDiem(string wsLink, string username, string password)
        {
            try
            {
                G02TravelWebService.WSTravel service = new G02TravelWebService.WSTravel();
                service.Url = wsLink;
                service.CookieContainer = new System.Net.CookieContainer();
                service.WSTravel0(username, password);

                G02TravelWebService.Location[] result = service.WSTravel_Location();
                if (result.Length != 0)
                {
                    List<LocationDTO> locationLst = new List<LocationDTO>();
                    for (int i = 0; i < result.Length; i++)
                    {
                        LocationDTO location = new LocationDTO();
                        location.Country = result[i].Country;
                        //location.LocationId = result[i].LocationId;
                        location.LocationName = result[i].LocationName;
                        locationLst.Add(location);
                    }
                    return locationLst;
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }


    public class ListTour
    {
        private string _tripId;

        public string TripId
        {
            get { return _tripId; }
            set { _tripId = value; }
        }
        private string _tourName;

        public string TourName
        {
            get { return _tourName; }
            set { _tourName = value; }
        }
        private string _visitLocation;

        public string VisitLocation
        {
            get { return _visitLocation; }
            set { _visitLocation = value; }
        }
        private string _departurePosition;

        public string DeparturePosition
        {
            get { return _departurePosition; }
            set { _departurePosition = value; }
        }
        private string _tourType;

        public string TourType
        {
            get { return _tourType; }
            set { _tourType = value; }
        }
        private string _tourImage;

        public string TourImage
        {
            get { return _tourImage; }
            set { _tourImage = value; }
        }
        private string _description;

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        private string _dayNight;

        public string DayNight
        {
            get { return _dayNight; }
            set { _dayNight = value; }
        }
        private DateTime _startDay;

        public DateTime StartDay
        {
            get { return _startDay; }
            set { _startDay = value; }
        }
        private DateTime _endDay;

        public DateTime EndDay
        {
            get { return _endDay; }
            set { _endDay = value; }
        }
        private int _price;

        public int Price
        {
            get { return _price; }
            set { _price = value; }
        }
        private string _unit;

        public string Unit
        {
            get { return _unit; }
            set { _unit = value; }
        }
        private string _maxQuatity;

        public string MaxQuatity
        {
            get { return _maxQuatity; }
            set { _maxQuatity = value; }
        }
    }
}
